package com.salton123.whiteboard.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.SeekBar;

import com.lxj.xpopup.core.BottomPopupView;
import com.salton123.util.ScreenUtils;
import com.salton123.whiteboard.Constant;
import com.salton123.whiteboard.R;
import com.salton123.whiteboard.model.bean.StrokeType;

import androidx.annotation.NonNull;

/**
 * User: wujinsheng1@yy.com
 * Date: 2020/5/27 15:09
 * ModifyTime: 15:09
 * Description:
 */
public class StrokePopup extends BottomPopupView {
    private ImageView strokeImageView, strokeAlphaImage;//画笔宽度，画笔不透明度，橡皮擦宽度IV
    private SeekBar strokeSeekBar, strokeAlphaSeekBar;
    RadioGroup strokeTypeRG, strokeColorRG;
    private float strokeSize = ScreenUtils.dp2px(5);
    public int strokeType = StrokeType.STROKE_TYPE_GRAFFITO;

    @Override
    protected int getImplLayoutId() {
        return R.layout.popup_sketch_stroke;
    }

    public StrokePopup(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        //画笔宽度缩放基准参数
        Drawable circleDrawable = getResources().getDrawable(R.drawable.circle);
        if (circleDrawable != null) {
            strokeSize = circleDrawable.getIntrinsicWidth();
        }

        strokeImageView = findViewById(R.id.stroke_circle);
        strokeAlphaImage = findViewById(R.id.stroke_alpha_circle);
        strokeSeekBar = findViewById(R.id.stroke_seekbar);
        strokeAlphaSeekBar = findViewById(R.id.stroke_alpha_seekbar);
        //画笔颜色
        strokeTypeRG = findViewById(R.id.stroke_type_radio_group);
        strokeColorRG = findViewById(R.id.stroke_color_radio_group);
        strokeTypeRG.setOnCheckedChangeListener((group, checkedId) -> {

            if (checkedId == R.id.stroke_type_rbtn_draw) {
                strokeType = StrokeType.STROKE_TYPE_GRAFFITO;
            } else if (checkedId == R.id.stroke_type_rbtn_line) {
                strokeType = StrokeType.STROKE_TYPE_LINE;
            } else if (checkedId == R.id.stroke_type_rbtn_circle) {
                strokeType = StrokeType.STROKE_TYPE_CIRCLE;
            } else if (checkedId == R.id.stroke_type_rbtn_rectangle) {
                strokeType = StrokeType.STROKE_TYPE_RECTANGLE;
            } else if (checkedId == R.id.stroke_type_rbtn_text) {
                strokeType = StrokeType.STROKE_TYPE_TEXT;
            } else {
                strokeType = StrokeType.STROKE_TYPE_GRAFFITO;
            }
            if (mCallback != null) {
                mCallback.onStrokeType(strokeType);
            }
            dismiss();//切换画笔后隐藏
        });
        strokeColorRG.setOnCheckedChangeListener((group, checkedId) -> {
            int color = Constant.INSTANCE.getCOLOR_BLACK();
            if (checkedId == R.id.stroke_color_black) {
                color = Constant.INSTANCE.getCOLOR_BLACK();
            } else if (checkedId == R.id.stroke_color_red) {
                color = Constant.INSTANCE.getCOLOR_RED();
            } else if (checkedId == R.id.stroke_color_green) {
                color = Constant.INSTANCE.getCOLOR_GREEN();
            } else if (checkedId == R.id.stroke_color_orange) {
                color = Constant.INSTANCE.getCOLOR_ORANGE();
            } else if (checkedId == R.id.stroke_color_blue) {
                color = Constant.INSTANCE.getCOLOR_BLUE();
            }
            if (mCallback != null) {
                mCallback.onStrokeColor(color);
            }
        });

        //画笔不透明度拖动条
        strokeAlphaSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                                          boolean fromUser) {
                int alpha = (progress * 255) / 100; //百分比转换成256级透明度
                strokeAlphaImage.setImageAlpha(alpha);
                if (mCallback != null) {
                    mCallback.onStrokeAlpha(alpha);
                }
            }
        });
        strokeAlphaSeekBar.setProgress(Constant.DEFAULT_STROKE_ALPHA);

        //画笔宽度拖动条
        strokeSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                                          boolean fromUser) {
                int calcProgress = Math.max(progress, 1);
                int newSize = Math.round((strokeSize / 100f) * calcProgress);
                int offset = Math.round((strokeSize - newSize) / 2);
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(newSize, newSize);
                lp.setMargins(offset, offset, offset, offset);
                strokeImageView.setLayoutParams(lp);
                mCallback.onStrokeSize(newSize);
            }
        });
        strokeSeekBar.setProgress((int) Constant.DEFAULT_STROKE_SIZE);
    }

    private OnStrokeCallback mCallback;

    public void setCallback(OnStrokeCallback callback) {
        this.mCallback = callback;
    }

    public interface OnStrokeCallback {
        void onStrokeType(int strokeType);

        void onStrokeColor(int color);

        void onStrokeAlpha(int alpha);

        void onStrokeSize(float newSize);
    }
}
